/**   
* @Title: ReflectHelper.java
* @Package org.xaoyaoyao.cloud.jpa.utils
* @Description: TODO
* @author xaoyaoyao
* @date Feb 13, 2019 9:44:59 AM
* @version V1.0   
*/
package org.xaoyaoyao.cloud.jpa.utils;

import java.lang.reflect.Field;

import javax.persistence.Id;

/**
 * @ClassName: ReflectHelper
 * @Description: TODO
 * @author xaoyaoyao
 * @date Feb 13, 2019 9:44:59 AM
 * 
 */
public class ReflectHelper {

	/**
	 * @Title: getIdField
	 * @Description: 获取实体类的字段信息
	 * @param clazz
	 *            实体类
	 * @return Field 字段集合
	 */
	public static Field getIdField(Class<?> clazz) {
		Field[] fields = clazz.getDeclaredFields();
		Field item = null;
		for (Field field : fields) {
			// 获取实体类中标识@Id的字段
			Id id = field.getAnnotation(Id.class);
			if (id != null) {
				field.setAccessible(true);
				item = field;
				break;
			}
		}
		if (item == null) {
			Class<?> superclass = clazz.getSuperclass();
			if (superclass != null) {
				item = getIdField(superclass);
			}
		}
		return item;
	}

}
